package com.lwb.ztx.videos.wedget;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lwb.ztx.R;
import com.lwb.ztx.api.Api;
import com.lwb.ztx.bean.VideoMainBean;
import com.lwb.ztx.bean.VideoTypeBean;
import com.lwb.ztx.utils.LogUtils;
import com.lwb.ztx.videos.presenter.VideoPresenter;
import com.lwb.ztx.videos.presenter.VideoPresenterImpl;
import com.lwb.ztx.videos.view.VideoView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class VideoTypeDetailActivity extends SwipeBackActivity implements VideoView,
        XRecyclerView.LoadingListener {
    @InjectView(R.id.video_type_detail_recycler_view)
    XRecyclerView recyclerView;
    private VideoPresenter presenter;
    private VideoTypeBean bean;
    private VideoAdapter adapter;
    @InjectView(R.id.back)
    View back;
    @InjectView(R.id.title)
    TextView title;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntent();
        setContentView(R.layout.activity_video_type_detail);
        ButterKnife.inject(this);
        title.setText(bean.getName());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new VideoAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(this);
        presenter = new VideoPresenterImpl(this);
        if (bean != null)
            presenter.getVideos(Api.getVideoUrlByType(1, bean.getId()), 1);
    }

    private void initIntent() {
        bean = (VideoTypeBean) this.getIntent().getSerializableExtra("videoType");
    }

    @Override
    public void setVideos(VideoMainBean bean) {
        LogUtils.i(bean.toString());
        adapter.setBeans(bean.getBeans());
        adapter.notifyDataSetChanged();
        page = 1;
        recyclerView.refreshComplete();
    }

    @Override
    public void addMoreVideos(VideoMainBean bean) {
        adapter.addMoreBeans(bean.getBeans());
        adapter.notifyDataSetChanged();
        recyclerView.loadMoreComplete();
    }

    @OnClick(R.id.back)
    public void onBackClick(View view) {
        this.finish();
    }

    @Override
    public void onRefresh() {
        LogUtils.i("refresh");
        if (bean != null) {
            LogUtils.i("refresh not null");
            presenter.getVideos(Api.getVideoUrlByType(1, bean.getId()), 1);
        }
    }

    @Override
    public void onLoadMore() {
        if (bean != null)
            page++;
        presenter.getVideos(Api.getVideoUrlByType(page, bean.getId()), page);
    }
}
